﻿namespace Raven
{
    public class GetWeaponGoalEvaluator : GoalEvaluator
    {
        private EntityType _entityType;
        public GetWeaponGoalEvaluator(Fix characterBias, EntityType entityType) : base(characterBias)
        {
            _entityType = entityType;
        }

        public override Fix CalculateDesirability(RavenBot pBot)
        {
            //first grab the distance to the closest instance of a health item
            Fix Distance = RavenFeature.DistanceToItem(pBot, _entityType);

            //if the distance feature is rated with a value of 1 it means that the
            //item is either not present on the map or too far away to be worth 
            //considering, therefore the desirability is zero
            if (Distance == 1)
            {
                return 0;
            }
            else
            {
                //value used to tweak the desirability
                Fix Tweaker = Fix.Ratio(2, 10);
  
                //the desirability of finding a health item is proportional to the amount
                //of health remaining and inversely proportional to the distance from the
                //nearest instance of a health item.
                Fix Desirability = Tweaker * (1-RavenFeature.Health(pBot)) / RavenFeature.DistanceToItem(pBot, EntityType.Health);
 
                //ensure the value is in the range 0 to 1
                Desirability = FixMath.Clamp(Desirability, 0, 1);
  
                //bias the value according to the personality of the bot
                Desirability *= _characterBias;

                return Desirability;
            }
        }

        public override void SetGoal(RavenBot pBot)
        {
            pBot.GetBrain().AddGoalGetItem(_entityType);
        }
    }
}